package com.kong.universal;



import org.burgeon.commons.react.Result;
import org.burgeon.commons.runtime.ThreadContextHolder;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;


@Configuration
public class ErrorPageRouter implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage[] errorPages = new ErrorPage[2];
        errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
        errorPages[1] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
        registry.addErrorPages(errorPages);
    }


    @RequestMapping(value = "/error/404",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Result<String> Error404(HttpServletResponse response) {
        response.setStatus(404);
        return Result.data(-4, "页面未找到", "页面未找到");
    }

    @RequestMapping(value = "/error/500",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Result<String> Error500(HttpServletResponse response) {
        response.setStatus(500);
        return Result.data(-4, ThreadContextHolder.getCurrentError(), "程序执行错误");
    }


}
